/*
 * Coder.java
 * classes : com.hymobile.security.Coder
 * @author 魏铮铮
 * V 1.0.0
 * Create at 2011-9-16 下午12:36:53
 */
package com.wzz.common.rsa;

import java.security.MessageDigest;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * com.hymobile.security.Coder 基础加密组件
 * 
 * @author 魏铮铮 <br/>
 *         create at 2011-9-16 下午12:36:53
 */

public abstract class Coder {
  public static final String KEY_SHA = "SHA";
  public static final String KEY_MD5 = "MD5";

  /**
   * MAC算法可选以下多种算法
   * 
   * <pre>
   * HmacMD5  
   * HmacSHA1  
   * HmacSHA256  
   * HmacSHA384  
   * HmacSHA512
   * </pre>
   */
  public static final String KEY_MAC = "HmacMD5";

  /**
   * BASE64解密
   * 
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] decryptBASE64(String key) throws Exception {
    return Base64.decode(key);
  }

  /**
   * BASE64加密
   * 
   * @param key
   * @return
   * @throws Exception
   */
  public static String encryptBASE64(byte[] key) throws Exception {
    return Base64.encodeString(key);
  }

  /**
   * MD5加密
   * 
   * @param data
   * @return
   * @throws Exception
   */
  public static byte[] encryptMD5(byte[] data) throws Exception {

    MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
    md5.update(data);
    return md5.digest();
  }

  public static String getMD5Str(String str) throws Exception {
    MessageDigest messageDigest = MessageDigest.getInstance(KEY_MD5);
    messageDigest.reset();
    messageDigest.update(str.getBytes("UTF-8"));

    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (int i = 0; i < byteArray.length; i++) {
      if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
        md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
      } else {
        md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
      }
    }
    return md5StrBuff.toString();
  }

  /**
   * SHA加密
   * 
   * @param data
   * @return
   * @throws Exception
   */
  public static byte[] encryptSHA(byte[] data) throws Exception {
    MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
    sha.update(data);
    return sha.digest();
  }

  /**
   * 初始化HMAC密钥
   * 
   * @return
   * @throws Exception
   */
  public static String initMacKey() throws Exception {
    KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);

    SecretKey secretKey = keyGenerator.generateKey();
    return encryptBASE64(secretKey.getEncoded());
  }

  /**
   * HMAC加密
   * 
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] encryptHMAC(byte[] data, String key) throws Exception {

    SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
    Mac mac = Mac.getInstance(secretKey.getAlgorithm());
    mac.init(secretKey);

    return mac.doFinal(data);

  }
}